<#
Math.Net PowerShell Module

Author
	Firaz Samet

	History:
		2013/06/27:First checked-in version 
#>

$ElementType="Single"
$MatrixType = "SparseMatrix"
$MathNetNumLA = "MathNet.Numerics.LinearAlgebra"
$NetMatrixType = [type]"$MathNetNumLA.$script:ElementType.$script:MatrixType"

function Set-MathNetMatrixType
{
param([string]$ElType = "Single", [string]$MatType = "SparseMatrix")

	$script:ElementType = $ElType
    $script:MatrixType = $MatType
    $script:NetMatrixType = [type]"$script:MathNetNumLA.$script:ElementType.$script:MatrixType"
}

function New-MathNetMatrix
{
param(
    [string]$elements,
    [string]$matrixFile,
	[int] $rows,
	[int] $columns
)
    if($rows -eq 0 -or $columns -eq 0)
    {
        $d = New-Object "$script:MathNetNumLA.$script:ElementType.IO.DelimitedReader``1[$($script:NetMatrixType.FullName)]"
        if([string]::IsNullOrEmpty($matrixFile))
        {
            $b=[System.Text.Encoding]::ASCII.GetBytes($elements.Replace(';',"`r"))
            $ms = New-Object System.IO.MemoryStream $b,0,$b.Length 
            $m = $d.ReadMatrix($ms)
        }
        else
        {
            $m = $d.ReadMatrix($matrixFile)
        }
    }
    else
    {
        $m = New-Object $script:NetMatrixType.FullName $rows, $columns
    }
    return $m
}

function New-MathNetMatrixIdentity
{
param([int] $order)
    $id = $script:NetMatrixType.DeclaredMethods | ? Name -eq Identity
    return $id.Invoke($null, $order)
}


function Get-MathNetMatrixDeterminant
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
		ValueFromPipeline=$True)]
[array]$matrices)
  PROCESS{
    Foreach ($matrix in $matrices){
      $matrix.Determinant()
    }
  }
}

function Get-MathNetMatrixTrace
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
		ValueFromPipeline=$True)]
[array]$matrices)
  PROCESS{
    Foreach ($matrix in $matrices){
      $matrix.Trace()
    }
  }
}

function Get-MathNetMatrixInverse
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
		ValueFromPipeline=$True)]
[array]$matrices)
  PROCESS{
    Foreach ($matrix in $matrices){
      $matrix.Inverse()
    }
  }
}

function Get-MathNetMatrixTranspose
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
		ValueFromPipeline=$True)]
[array]$matrices)
  PROCESS{
    Foreach ($matrix in $matrices){
      $matrix.Transpose()
    }
  }
}


Set-Alias m New-MathNetMatrix
Set-Alias id New-MathNetMatrixIdentity
Set-Alias i New-MathNetMatrixIdentity
Set-Alias t Get-MathNetMatrixTranspose
Set-Alias trc Get-MathNetMatrixTrace
Set-Alias inv Get-MathNetMatrixInverse
Set-Alias d Get-MathNetMatrixDeterminant
Set-Alias det Get-MathNetMatrixDeterminant


Set-MathNetMatrixType

Export-ModuleMember -Function * -Alias *
